package smartcrud.spring.models.cms;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import smartcrud.common.orm.Page;
import smartcrud.common.orm.PropertyFilter;
import smartcrud.common.orm.jdbc.Sql;

@Repository
public class CategoryDaoJdbcImpl implements CategoryDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;
	@Override
	public Category createCategory(final Category o) {
		final StringBuffer sql = new StringBuffer("insert into CMS_CATEGORY(CODE,NAME,PID)");
		sql.append(" values(?,?,?)");
        
		KeyHolder keyHolder = new GeneratedKeyHolder();
		jdbcTemplate.update(/* sql, */new PreparedStatementCreator() {
			@Override
			public PreparedStatement createPreparedStatement(Connection con)
					throws SQLException {
				PreparedStatement ps = con.prepareStatement(sql.toString(),
						Statement.RETURN_GENERATED_KEYS);
				int z = 0;
				ps.setString(++z, o.getCode());
				ps.setString(++z, o.getName());
				ps.setLong(++z, o.getPid());
				return ps;
			}
		}, keyHolder);
		o.setId(keyHolder.getKey().longValue());
		return o;
	}

	@Override
	public void deleteCategory(Long id) {
		jdbcTemplate.update("delete from CMS_CATEGORY where ID=?", id);
	}

	@Override
	public void updateCategory(Category o) {
	    final StringBuffer sql = new StringBuffer("update CMS_CATEGORY set CODE=?,NAME=?,PID=?");
	    sql.append(" where ID=?");       
		jdbcTemplate.update(sql.toString(), 
				o.getCode(), 
				o.getName(), 
				o.getPid(), 
				o.getId() 
				);
	}

	@Override
	public Category findCategoryById(Long id) {
		final String sql = "select ID,CODE,NAME,PID from CMS_CATEGORY where ID=?";
		return jdbcTemplate.queryForObject(sql, new RowMapper<Category>() {
			@Override
			public Category mapRow(ResultSet rs, int rowNum) throws SQLException {
				Category o = new Category();
				o.setId(rs.getLong("ID"));
				o.setCode(rs.getString("CODE"));
				o.setName(rs.getString("NAME"));
				o.setPid(rs.getLong("PID"));
				return o;
			}
		}, id);
	}

	@Override
	public List<Category> findAllCategorys() {
        final String sql = "select ID,CODE,NAME,PID from CMS_CATEGORY";
		return jdbcTemplate.query(sql, new RowMapper<Category>() {
			@Override
			public Category mapRow(ResultSet rs, int rowNum) throws SQLException {
				Category o = new Category();
				o.setId(rs.getLong("ID"));
				o.setCode(rs.getString("CODE"));
				o.setName(rs.getString("NAME"));
				o.setPid(rs.getLong("PID"));
				return o;
			}
		});
	}
	
    @Override
    public int count() {
        return jdbcTemplate.queryForInt("select count(1) from CMS_CATEGORY");
    }
    
	@Override
	public int count(Page<Category> page,final List<PropertyFilter> filters) {
		Sql sql = new Sql("select count(1) from CMS_CATEGORY where 1=1 ", page,filters);
		sql.build4count();
		return jdbcTemplate.queryForInt(sql.getSqlBody(), sql.getSqlParams());
	}

	@Override
	public Page<Category> findByPage(Page<Category> page, final List<PropertyFilter> filters) {
		if (page.isAutoCount()) {
			int totalCount = count(page, filters);
			page.setTotalCount(totalCount);
		}
		
		Sql sql = new Sql("select * from CMS_CATEGORY where 1=1 ", page, filters);
		sql.build();

		List<Category> content = jdbcTemplate.query(sql.getSqlBody(),
				new RowMapper<Category>() {
					@Override
					public Category mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						Category o = new Category();
						o.setId(rs.getLong("ID"));
						o.setCode(rs.getString("CODE"));
						o.setName(rs.getString("NAME"));
						o.setPid(rs.getLong("PID"));
						return o;
					}
				}, sql.getSqlParams());

		page.setContent(content);
		return page;
	}

}
